package com.segersten.othello.model;

import java.util.Set;

public class DefaultNextColorStrategy implements NextColorStrategy
{
    Color expectedColor = Color.BLACK;

    @Override
    public void nextRoundAction(Color color, Set<Point> whiteAvailablePoints, Set<Point> blackAvailablePoints) throws IllegalActionException
    {
        if (color.equals(expectedColor)) {
           Color next = expectedColor.getOpposite();
           if (getAvailblePoints(next, whiteAvailablePoints, blackAvailablePoints).isEmpty()) {
               next = next.getOpposite();
           }
           if (getAvailblePoints(next, whiteAvailablePoints, blackAvailablePoints).isEmpty()) {
               throw new IllegalStateException("Wrong NextColorSelector - Game was finished");
           }
           expectedColor = next;
        } else {
            throw new IllegalActionException(String.format("Expected %s, got %s", expectedColor, color));
        }
    }

    @Override
    public NextColorStrategy stop()
    {
        return new FinishedGameNextColorStrategy();
    }
    
    private Set<Point> getAvailblePoints(Color color, Set<Point> whiteAvailablePoints, Set<Point> blackAvailablePoints)
    {
        return color.equals(Color.WHITE) ? whiteAvailablePoints : blackAvailablePoints;
    }

    @Override
    public Color getExpectedColor()
    {
        return expectedColor;
    }

}
